﻿// TestClient.Web.DownloadHandler.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DownloadHandler.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/16/2011
// ----------------------------------------------------------------------------
using System;
using System.IO;
using HSS.IO.Zip;
using HSS.Logging;

namespace TestClient.Web
{
	public class DownloadHandler : HSS.Interlink.Web.BaseDownloadHandler
	{
		string basePath;

		public DownloadHandler()
		{
			this.basePath = this.GetFolder(UploadHandler.FileStoreFolder);
			// Uncomment this line to turn off purging of temp files.
			//this.PurgeInterval = 0;
		}

		public override bool OpenFileInline()
		{
			string fileName = Path.Combine(this.basePath, this.RequestedFile);
			FileInfo fi = new FileInfo(fileName);
			if (fi.Exists)
			{
				this.SendFile(fi.FullName, fi.Length);
				return true;
			}
			else
				return false;
		}

		public override bool OpenFileAsAttachment()
		{
			string fileName = Path.Combine(this.basePath, this.RequestedFile);
			FileInfo fi = new FileInfo(fileName);
			if (fi.Exists)
			{
				this.SendFileAsAttachment(fi.FullName, fi.Length);
				return true;
			}
			else
				return false;
		}

		public override bool GetSingleFile()
		{
			string fileName = Path.Combine(this.basePath, this.RequestedFile);
			FileInfo fi = new FileInfo(fileName);
			if (fi.Exists)
			{
				this.SendFile(fi.FullName, fi.Length);
				return true;
			}
			else
				return false;
		}

		public override bool GetMultipleFiles()
		{
			string archiveName = String.Format("tempFile-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
			string tempFile = this.GetTempFile(".zip");

			using (ZipFile zip = new ZipFile(archiveName))
			{
				zip.TempFileFolder = this.TempFolder;
				foreach (string file in this.RequestedFiles)
				{
					string fileName = Path.Combine(this.basePath, file);
					if (File.Exists(fileName))
						zip.AddFile(fileName, string.Empty);
				}
				zip.Save(tempFile);
			}

			var fi = new FileInfo(tempFile);
			this.SendFile(fi.FullName, fi.Length, "application/zip");

			return true;
		}

		public override bool IsAuthorized()
		{
			#region Test Authorization
			//if (this.Context.User.Identity.IsAuthenticated)
			//{
			//    if (this.Context.User.IsInRole("requiredRole"))
			//        return true;
			//}
			//return false;
			#endregion

			return true;
		}

		public override int OnError(Exception ex)
		{
			// Log the error
			HSS.Logging.Log.WriteException(ex, this.UserName, "DownloadHandler", "TestClient");

			// And specify your desired HTTP Response
			// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
			return 500; // 500 = Internal Server Error
		}
	}
}